package com.sskj.hangqing.ui.activity;


import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.LanguageUtil;
import com.sskj.common.widget.DensityWebView;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.bean.GuideListBean;
import com.sskj.hangqing.bean.HelpBean;
import com.sskj.hangqing.http.HttpConfig;
import com.sskj.hangqing.presenter.GuideWebActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;

@Route(path = RConfig.HANG_GUIDE_WEB)
public class GuideWebActivity extends BaseActivity<GuideWebActivityPresenter> {
    @BindView(R2.id.tvContent)
    TextView tvContent;
    @BindView(R2.id.llHead)
    LinearLayout llHead;
    @BindView(R2.id.webView)
    DensityWebView webView;

    @Autowired
    String content;
    @Autowired
    String time;
    @Autowired
    String title;
    @Autowired
    String head;

    @Autowired
    boolean isHelp = false;//帮助中心
    @Autowired
    boolean isBibi = false;//币币
    @Autowired
    boolean isFabi = false;//fabi
    @Autowired
    boolean isAboutUs = false;//关于我们
    @Autowired
    boolean isZixun = false;//资讯
    @Autowired
    boolean isPrivate = false;//隐私条款
    @BindView(R2.id.tvWebTitle)
    TextView tvWebTitle;
    @BindView(R2.id.tvTime)
    TextView tvTime;
    @Autowired
    boolean isRegisterAgreement = false;//服务协议
    @Autowired
    boolean isHangqingNotice = false;//行情公告
    @Autowired
    boolean isTradeGuide = false;//交易指南
    @Autowired
    boolean isGuset = false;//客服


    @Override
    protected int getLayoutId() {
        return R.layout.hang_activity_guide_web;
    }

    @Override
    public GuideWebActivityPresenter getPresenter() {
        return new GuideWebActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);


        if (isHelp) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity1));
            llHead.setVisibility(View.GONE);
            tvContent.setVisibility(View.GONE);
            webView.setVisibility(View.VISIBLE);
//            webView.setBackgroundColor(0);
//            webView.getBackground().setAlpha(0);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebChromeClient(new WebChromeClient());
            webView.setWebViewClient(new WebViewClient());
            webView.loadUrl(HttpConfig.BASE_URL + "/help/index.html?language=" + (LanguageUtil.isSimpleChinese() ? "cn" : "eu"));
            webView.setOnKeyListener((v, keyCode, event) -> {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            });
//            mPresenter.help("12");
        } else if (isBibi) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity2));
            mPresenter.help("11");
        } else if (isFabi) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity3));
            mPresenter.help("10");
        } else if (isAboutUs) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity4));
            mPresenter.aboutUs();
        } else if (isRegisterAgreement) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity5));
            mPresenter.registerAgreement();
        } else if (isPrivate) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity6));
            mPresenter.privateService();
        } else if (isZixun) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity7));
            updateUI();
        } else if (isHangqingNotice) {
            setTitle(head);
            if (TextUtils.isEmpty(content)) {
                return;
            }
            tvWebTitle.setText(title);
            tvTime.setText(time);
            tvContent.setVisibility(View.GONE);
            webView.getSettings().setDefaultTextEncodingName("utf-8");
            webView.setVisibility(View.VISIBLE);
////            webView.setBackgroundColor(ContextCompat.getColor(App.INSTANCE,R.color.hang_2d3));
//            webView.getBackground().setAlpha(0);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebChromeClient(new WebChromeClient());
            webView.setWebViewClient(new WebViewClient());
            webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
            webView.setOnKeyListener((v, keyCode, event) -> {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            });
            updateUI();
        } else if (isTradeGuide) {
            setTitle(head);
            updateUI();
        } else if (isGuset) {
            setTitle(App.INSTANCE.getString(R.string.hangqingguideWebActivity8));
            llHead.setVisibility(View.GONE);
            tvContent.setVisibility(View.GONE);
            webView.setVisibility(View.VISIBLE);
//            webView.setBackgroundColor(0);
//            webView.getBackground().setAlpha(0);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebChromeClient(new WebChromeClient());
            webView.setWebViewClient(new WebViewClient());
            webView.loadUrl("https://www.365webcall.com/chat/ChatWin3.aspx?settings=mw7m6NbN6Pw0I7z3A7wbb6z3AmwXX0bz3AX6mmPb");
            webView.setOnKeyListener((v, keyCode, event) -> {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            });
        }

    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();

        } else {
            super.onBackPressed();
        }
    }

    public void updateUI() {
        if (TextUtils.isEmpty(content)) {
            return;
        }
        tvWebTitle.setText(title);
        tvTime.setText(time);

        RichText.from(content).into(tvContent);
    }


    public void getContentSuccess(GuideListBean data) {
        content = data.getContent();
        time = data.getCreateTime();
        title = data.getTitle();
        tvWebTitle.setText(title);
        tvTime.setText(time);
        updateUI();
    }

    public void updateContent(String data) {
        this.content = data;
        updateUI();
    }


    public void updateHelp(HelpBean data) {
        llHead.setVisibility(View.GONE);
        if (data != null) {
            content = data.getContent();
        } else {
            content = "";
        }
        updateUI();
    }

    public void updatePrivate(HelpBean data) {
        llHead.setVisibility(View.GONE);
        if (data != null) {
            content = data.getContent();
        } else {
            content = "";
        }
        updateUI();
    }
}
